// pages/teamDetail/teamDetail.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    team:null,
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    var that = this
    var teamid = app.globalData.teamID
    var userid = null;
    if (app.globalData.hasUserInfo){
      userid = app.globalData.userInfo.openId
    }
    wx.request({
      url: 'http://' + app.globalData.ip + '/team/getTeamById',
      data: { userid: userid, teamid: teamid },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        var t = res.data.team
        var d = new Date(t['createTime'])
        t['createTime'] = d.getFullYear() + '.' + d.getMonth() + '.' + d.getDate()
        that.setData({
          team: t
        })
      }
    })
  },

  /*预约功能 */
  reserve:function(){
    console.log("我预约了")
    if (app.globalData.hasUserInfo) {
      //用户已授权
      var that = this
      var hasNumber = "team.hasNumber"
      var myreserve = "team.reserve"
      if (this.data.team.reserve == true) {
        wx.request({
          url: 'http://' + app.globalData.ip + '/reservation/cancleReservation',
          data: { userid: app.globalData.userInfo.openId, teamid: that.data.team.teamID },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function (res) {
            if (res.data.msg == 1) {
              wx.showToast({
                title: '取消预约',
              })
              that.setData({
                [myreserve]: false,
                [hasNumber]: res.data.hasNumber
              })
            } else {
              wx.showModal({
                content: '服务端取消预约失败',
              })
            }
          },
          fail: function () {
            wx.showModal({
              content: '取消预约失败',
            })
          }
        })
      } else {
        wx.request({
          url: 'http://' + app.globalData.ip + '/reservation/addReservation',
          data: { userid: app.globalData.userInfo.openId, teamid: that.data.team.teamID },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function (res) {
            console.log(res)
            if (res.data.msg == 1) {             
              wx.showToast({
                title: '预约成功',
              })
              that.setData({
                [myreserve]: true,
                [hasNumber]: res.data.hasNumber
              })
            } else if (res.data.msg == 0){
              wx.showModal({
                content: '人数已满'
              })
            }else{
              wx.showModal({
                content: '服务器出差了',
              })
            }
          },
          fail: function () {
            wx.showModal({
              content: '预约失败'
            })
          }
        })
      }
    } else {
      //用户未授权
      wx.showModal({
        content: '请前往授权',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '../index/index',
            })
          }
        }
      })
    }
    console.log(this.data.team)
  },

  /**点击收藏功能 */
  like: function (e) {
    if(app.globalData.hasUserInfo){
      //用户已授权
      var that = this
      var mylike = "team.like"
      if (this.data.team.like == true) {
        //原先为true表示收藏，则点击时表示取消收藏     
        /**向后端发送请求 取消当前的收藏 */
        wx.request({
          url: 'http://' + app.globalData.ip +'/like/cancleLike',
          data: { userid: app.globalData.userInfo.openId, teamid: that.data.team.teamID },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function (res) {
            if (res.data.msg == 1) {
              wx.showToast({
                title: '取消收藏',
              })
              that.setData({
                [mylike]: false
              })
            } else {
              wx.showModal({
                content: '服务端取消收藏失败',
              })
            }
          },
          fail: function () {
            wx.showModal({
              content: '取消收藏失败',
            })
          }
        })
      } else {
        /**向后端发送请求将当前的收藏 */
        wx.request({
          url: 'http://' + app.globalData.ip +'/like/addLike',
          data: { userid: app.globalData.userInfo.openId, teamid: that.data.team.teamID },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function (res) {
            if (res.data.msg == 1) {
              wx.showToast({
                title: '收藏成功',
              })
              that.setData({
                [mylike]: true
              })
            } else {
              wx.showModal({
                content: '服务端收藏失败'
              })
            }
          },
          fail: function () {
            wx.showModal({
              content: '收藏失败'
            })
          }
        })
      }
    }else{
      //用户未授权
      wx.showModal({
        content: '请前往授权',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '../index/index',
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})